<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Checks if or where headers have been sent</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.headers-list.html">headers_list</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.http-response-code.html">http_response_code</a></div>
 <div class="up"><a href="ref.network.html">Network 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.headers-sent" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">headers_sent</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">headers_sent</span> &mdash; <span class="dc-title">Checks if or where headers have been sent</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.headers-sent-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>headers_sent</strong></span>
    ([ <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$file</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$line</code></span>
  ]] )</div>

  <p class="para rdfs-comment">
   Checks if or where headers have been sent.
  </p>
  <p class="para">
   You can&#039;t add any more header lines using the <span class="function"><a href="function.header.html" class="function">header()</a></span>
   function once the header block has already been sent. Using this function
   you can at least prevent getting HTTP header related error messages.
   Another option is to use <a href="ref.outcontrol.html" class="link">Output Buffering</a>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.headers-sent-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">file</code></em></dt>

     <dd>

      <p class="para">
       If the optional <em><code class="parameter">file</code></em> and
       <em><code class="parameter">line</code></em> parameters are set, 
       <span class="function"><strong>headers_sent()</strong></span> will put the PHP source file name
       and line number where output started in the <em><code class="parameter">file</code></em>
       and <em><code class="parameter">line</code></em> variables.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">line</code></em></dt>

     <dd>

      <p class="para">
       The line number where the output started.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.headers-sent-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>headers_sent()</strong></span> will return <strong><code>FALSE</code></strong> if no HTTP headers
   have already been sent or <strong><code>TRUE</code></strong> otherwise.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.headers-sent-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>4.3.0</td>
       <td>
        The optional <em><code class="parameter">file</code></em> and <em><code class="parameter">line</code></em>
        parameters were added.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.headers-sent-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4394">
    <p><strong>Example #1 Examples using <span class="function"><strong>headers_sent()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;If&nbsp;no&nbsp;headers&nbsp;are&nbsp;sent,&nbsp;send&nbsp;one<br /></span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">headers_sent</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Location:&nbsp;http://www.example.com/'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;An&nbsp;example&nbsp;using&nbsp;the&nbsp;optional&nbsp;file&nbsp;and&nbsp;line&nbsp;parameters,&nbsp;as&nbsp;of&nbsp;PHP&nbsp;4.3.0<br />//&nbsp;Note&nbsp;that&nbsp;$filename&nbsp;and&nbsp;$linenum&nbsp;are&nbsp;passed&nbsp;in&nbsp;for&nbsp;later&nbsp;use.<br />//&nbsp;Do&nbsp;not&nbsp;assign&nbsp;them&nbsp;values&nbsp;beforehand.<br /></span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">headers_sent</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$linenum</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Location:&nbsp;http://www.example.com/'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br /><br /></span><span style="color: #FF8000">//&nbsp;You&nbsp;would&nbsp;most&nbsp;likely&nbsp;trigger&nbsp;an&nbsp;error&nbsp;here.<br /></span><span style="color: #007700">}&nbsp;else&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Headers&nbsp;already&nbsp;sent&nbsp;in&nbsp;</span><span style="color: #0000BB">$filename</span><span style="color: #DD0000">&nbsp;on&nbsp;line&nbsp;</span><span style="color: #0000BB">$linenum</span><span style="color: #DD0000">\n"&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"Cannot&nbsp;redirect,&nbsp;for&nbsp;now&nbsp;please&nbsp;click&nbsp;this&nbsp;&lt;a&nbsp;"&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"href=\"http://www.example.com\"&gt;link&lt;/a&gt;&nbsp;instead\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 notes" id="refsect1-function.headers-sent-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
<p class="para">
 数据头只会在SAPI支持时得到处理和输出。
</p>
</p></blockquote>

 </div>


 <div class="refsect1 seealso" id="refsect1-function.headers-sent-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.ob-start.html" class="function" rel="rdfs-seeAlso">ob_start()</a> - 打开输出控制缓冲</span></li>
    <li class="member"><span class="function"><a href="function.trigger-error.html" class="function" rel="rdfs-seeAlso">trigger_error()</a> - 产生一个用户级别的 error/warning/notice 信息</span></li>
    <li class="member"><span class="function"><a href="function.headers-list.html" class="function" rel="rdfs-seeAlso">headers_list()</a> - Returns a list of response headers sent (or ready to send)</span></li>
    <li class="member">
     <span class="function"><a href="function.header.html" class="function" rel="rdfs-seeAlso">header()</a> - Send a raw HTTP header</span> for a more detailed discussion of the
     matters involved.
    </li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.headers-list.html">headers_list</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.http-response-code.html">http_response_code</a></div>
 <div class="up"><a href="ref.network.html">Network 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
